<?php
include_once 'CtFile.class.php';
/**
 * 视频处理类
 * @author chengtao05011633@gmail.com
 * @veasion 0.0.1
 */
class CtVideo extends CtFile{
	private $movie_obj = null;
	/**
	 * 集成父类的构造函数
	 */
	public function __construct($file){
		parent::__construct($file);
		if($this->isFile()){
			try{
				$this->movie_obj = new ffmpeg_movie($this->file);
			}catch(Exception $e ){
				echo $e->getMessage();
				echo "\n";
				exit;
			}
		}else{
			$this->setError('File is not exist!');
		}
	}
	/**
	 * 调用不存在的方法
	 */
	public function __call($functionName,$args){
		$movie_write_list = array(
			'getDuration',		//获取视频时长
			'getFrameCount',	//获取视频帧的个数
			'getFrameRate',		//获取帧率
			'getFilename',		//获取文件名
			'getComment',		//获取文件描述
			'getTitle',			//获取标题
			'getAuthor',		//getArtist的别名
			'getCopyright',		//Return the copyright field from the movie or audio file.
			'getArtist',		//Return the artist ID3 field from an mp3 file.
			'getGenre',			//Return the genre ID3 field from an mp3 file.
			'getTrackNumber',	//Return the track ID3 field from an mp3 file.
			'getYear',			//Return the year ID3 field from an mp3 file.
			'getFrameHeight',	//获取帧的高度
			'getFrameWidth',	//获取真的宽度
			'getPixelFormat',	//Return the pixel format of the movie.
			'getBitRate',		//获取比特率
			'getVideoBitRate',	//获取视频比特率
			'getAudioBitRate',	//获取音频比特率
			'getAudioSampleRate',//Return the audio sample rate of the media file in bits per second.
			'getFrameNumber',	//Return the current frame index
			'getVideoCodec',	//获取视频编码
			'getAudioCodec',	//获取音频编码
			'getAudioChannels',	//Return the number of audio channels in this movie as an integer.
			'hasAudio',			//Return boolean value indicating whether the movie has a audio stream.
			'hasVideo',			//Return boolean value indicating whether the movie has an video stream.
			'getFrame',			//根据时间获取真
			'getNextKeyFrame',	//获取下一个关键帧
		);
		if(in_array($functionName,$movie_write_list)){
			try{
				if($functionName == 'getFrame'){
					return $this->movie_obj->getFrame($args[0]);
				}else{
					if(method_exists($this->movie_obj,$functionName)){
						return $this->movie_obj->{$functionName}();
					}else{
						return 0;
					}
				}
			}catch(Exception $e ){
				echo $e->getMessage();
				echo "\n";
			}
		}
	}
    public static function getVideoInfo($file){
        $re = array();
        exec("ffmpeg -i {$file} 2>&1", $re);
        $info = implode("\n", $re);
        
        if(preg_match("/Invalid data/i", $info)){
            return false;
        }
        
        $match = array();
        preg_match("/\d{2,}x\d+/", $info, $match);
        list($width, $height) = explode("x", $match[0]);

        
        $match = array();
        preg_match("/Duration:(.*?),/", $info, $match);
        $duration = date("H:i:s", strtotime($match[1]));
        
        $match = array();
        preg_match("/bitrate:(.*kb\/s)/", $info, $match);
        $bitrate = $match[1];
        

		$width = intval($width);
		$height = intval($height);
		$duration_list = array_filter(explode(':',$duration));

		$duration=0;
		$duration += $duration_list[0]*3600+$duration_list[1]*60 + $duration_list[2];


		$bitrate = intval(array_shift(array_filter(explode(' ',$bitrate))));

        if(!$width && !$height && !$duration && !$bitrate){
            return false;
        }else{
            return array(
                "width" => $width,
                "height" => $height,
                "duration" => $duration,
                "bitrate" => $bitrate,
            );
        }
    }


/*

ffmpeg_frame
Method	Description
$frame = new ffmpeg_frame(Resource gd_image)	Create a frame object from a GD image.
		NOTE: This function will not be available if GD is not enabled.
$frame->getWidth()	Return the width of the frame.
$frame->getHeight()	Return the height of the frame.
$frame->getPTS() alias $frame->getPresentationTimestamp()	Return the presentation time stamp of the frame.
$frame->resize(Integer width, Integer height [, Integer crop_top [, Integer crop_bottom [, Integer crop_left [, Integer crop_right ]]]])	Resize and optionally crop the frame. (Cropping is built into ffmpeg resizing so I'm providing it here for completeness.)
		width - New width of the frame (must be an even number).
		height - New height of the frame (must be an even number).
		croptop - Remove [croptop] rows of pixels from the top of the frame.
		cropbottom - Remove [cropbottom] rows of pixels from the bottom of the frame.
		cropleft - Remove [cropleft] rows of pixels from the left of the frame.
		cropright - Remove [cropright] rows of pixels from the right of the frame.
		NOTE: Cropping is always applied to the frame before it is resized. Crop values must be even numbers.
$frame->crop(Integer crop_top [, Integer crop_bottom [, Integer crop_left [, Integer crop_right ]]])	Crop the frame.
		croptop - Remove [croptop] rows of pixels from the top of the frame.
		cropbottom - Remove [cropbottom] rows of pixels from the bottom of the frame.
		cropleft - Remove [cropleft] rows of pixels from the left of the frame.
		cropright - Remove [cropright] rows of pixels from the right of the frame.
		NOTE: Crop values must be even numbers.
$frame->toGDImage()	Returns a truecolor GD image of the frame.
		NOTE: This function will not be available if GD is not enabled


ffmpeg_animated_gif
Method	Description
$gif = new ffmpeg_animated_gif(String output_file_path, Integer width, Integer height, Integer frame_rate, [Integer loop_count])	create a new ffmpeg_animated_gif object
	output_file_path - Location in the filesystem where the animated gif will be written.
	width - Width of the animated gif.
	height - Height of the animated gif.
	frame_rate - Frame rate of the animated gif in frames per second..
	loop_count - Number of times to loop the animation. Put a zero here to loop forever or omit this parameter to disable looping.
$frame->addFrame(ffmpeg_frame frame_to_add)	Add a frame to the end of the animated gif.
	frame_to_add - The ffmpeg_frame object to add to the end of the animated gif.

*/






}
 ?>